define(['moment'], function () {
    return ['$scope', '$rootScope', '$filter', '$stateParams', '$mdDialog', 'config', 'MMWService', '$calculater',
        function ($scope, $rootScope, $filter, $stateParams, $mdDialog, config, $MMWService, $calculater) {

            $scope.init = function () {
                $scope.list_filter = {};
                $scope.filterSH = {};
                $scope.transfer_data = [];
                $scope.transfer_data_detail = [];
                $scope.select_group_no = "";
                $scope.checked_len = 0;
                //
                $scope.query_product_no = "";
                $scope.query_lot_no = "";
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            //20170505 modify by Dustdusk for 入庫查詢可以不用輸入條件
            $scope.search = function () {
                $scope.list_filter = {};
                $scope.filterSH = {};
                $scope.transfer_data = [];
                $scope.transfer_data_detail = [];
                $scope.select_group_id = "";

                var content = {};
                content.type = "0";
                content.query_condition = {};
                content.query_condition.lot_no = $scope.query_lot_no;
                content.query_condition.item_no = $scope.query_product_no;
                mesh_stuck_processed_info_get(content, function (data) {
                    if (data) {
                        data.forEach(function (item) {
                            item.ischeck = false;
                            item.transfer_qty = item.qty;
                        });
                        $scope.transfer_data = data;
                    }
                });
            };
            $scope.confirm = function () {
                var submit_data = $filter('filter')($scope.transfer_data, { ischeck: true });
                if (submit_data && submit_data.length > 0) {
                    var content = {};
                    content.list = submit_data;
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_WIP.mesh_stuck_transfer_recovery',
                        content: content,
                        success: function (data) {
                            $scope.$root.showToast($filter('translate')('B1302.msg.process_success'));
                            $scope.search();
                        }
                    });
                }
                else {
                    $scope.$root.showAlert($filter('translate')('B1302.msg.lot_yet_click'));
                }
            };
            $scope.itemClick = function (index, item) {
                if (item.ischeck)
                    item.ischeck = false;
                else
                    item.ischeck = true;
                $scope.checked_len = $filter('filter')($scope.transfer_data, { ischeck: true }).length;
            }
            $scope.RowClick = function (index, item) {
                $scope.select_group_id = item.group_id;
                $scope.transfer_data_detail = [];
                var content = {};
                content.type = "1";
                content.query_condition = {};
                content.query_condition.group_id = item.group_id;
                mesh_stuck_processed_info_get(content, function (data) {
                    if (data && data.length > 0) {
                        $scope.transfer_data_detail = data;
                    }
                    else {
                        $scope.$root.showAlert($filter('translate')('B1302.msg.no_transfer_detail'));
                    }
                });
            }
            //取得產品編號
            $scope.loadItemNo = function () {

                $MMWService.showQueryListPNIG({
                    page_mode: 18,
                    pageRecord: 50,
                    key: 'page_no',
                    condition: [
                        { name: $filter('translate')('common.queryList.item_c'), column_name: 'B.ProductNo', query_mode: '2', value: $scope.query_product_no || '' },
                        { name: $filter('translate')('common.queryList.item_n'), column_name: 'B.ProductName', query_mode: '2', value: '' }
                    ],
                    header: [
                        { label: $filter('translate')('common.queryList.item_c'), code: 'page_no' },
                        { label: $filter('translate')('common.queryList.item_n'), code: 'page_name' }
                    ],
                    selectCode: $scope.query_product_no,
                    confirm: function (selectItem) {
                        $scope.query_product_no = selectItem.page_no;
                    }
                });
            };

            //取得生產批號
            $scope.loadlotNo = function () {
                $MMWService.showQueryListPNIG({
                    page_mode: "mesh_stuck_lot_info",
                    pageRecord: 50,
                    condition: [
                        { name: $filter('translate')('B1302.list.column05'), column_name: 'LOTNO', query_mode: '2', value: $scope.query_lot_no }
                    ],
                    header: [
                        { label: $filter('translate')('B1302.list.column05'), code: 'lotno' }
                    ],
                    setCondition: function (conditions) {
                        return conditions;
                    },
                    selectCode: $scope.query_lot_no,
                    confirm: function (selectItem) {
                        $scope.query_lot_no = selectItem.lotno;
                    }
                });
            };

            function mesh_stuck_processed_info_get(content, feedback) {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.mesh_stuck_processed_info_get',
                    content: content,
                    success: function (data) {
                        feedback(data.mesh_stuck_processed_info_get);
                    }
                });
            }

        }
    ];
});
